package logic

import (
	"context"
	"go_zero_list/mode"

	"go_zero_list/doctor_rpc/doctor_rpc"
	"go_zero_list/doctor_rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListLogic {
	return &ListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListLogic) List(in *doctor_rpc.ListRequest) (*doctor_rpc.ListResponse, error) {
	// todo: add your logic here and delete this line
	var doctors []mode.Doctor
	var Data doctor_rpc.ListResponse
	if err := mode.DB.Limit(10).Offset(int(in.Page-1) * 10).Find(&doctors).Error; err != nil {
		return &doctor_rpc.ListResponse{}, err
	}
	for _, doctor := range doctors {
		var Get doctor_rpc.DoctorStruct
		Get.Des = doctor.Des
		Get.Id = int64(doctor.ID)
		Get.Name = doctor.Name
		Get.Image = doctor.Image
		Get.Title = doctor.Title
		Get.FormHos = doctor.FormHos
		Get.FormDep = doctor.FormDep
		Data.Doctors = append(Data.Doctors, &Get)
	}
	if err := mode.DB.Model(&mode.Doctor{}).Pluck("count(*)", &Data.Total).Error; err != nil {
		return &doctor_rpc.ListResponse{}, err
	}
	return &Data, nil
}
